﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using System.IO;
using System.Data.Odbc;
using System.Data.OleDb;
using DevExpress.Web;

namespace Web.Pages.QuShui.DiXiaS._8.QiTaGL
{
    public partial class DaYinSJDC : BasePage
    {
        #region 属性
        
        #endregion

        #region 变量
        QS_YG_YONGHUXXBLL yhbll = new QS_YG_YONGHUXXBLL();
        #endregion

        #region 方法
        private void InitControl()
        {
            txtYear.Text = BusinessTool.GetSLYMorZWNY(1, BusinessTool.WType).ToString();
            txtLX.SelectedIndex = 0;
            txtQY.SelectedIndex = 0;
            DataTable dt = new DataTable();
            dt.Columns.Add("查询信息");
            gvList.DataSource = dt;
            gvList.DataBind();
            
        }
        #endregion

        #region 事件

        protected void txtQY_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnChaKan_Click(sender, e);
        }

        protected void btnChaKan_Click(object sender, EventArgs e)
        {
            int y = 0;
            if (!int.TryParse(txtYear.Text, out y))
            {
                return;
            }
            var count = gvList.Columns.Count;
            while (gvList.Columns.Count > 0)
            {
                gvList.Columns.RemoveAt(0);
            }


            DataSet ds = SearchSpHelper.P_QS_QT_DAYINSJDCCX(BusinessTool.WType, Convert.ToInt32(txtYear.Text), txtLX.SelectedIndex, Convert.ToInt32(txtQY.Value), null);
            double dvalue = 0;
            if (ds.Tables[0] != null)
            {
                foreach (DataColumn d in ds.Tables[0].Columns)
                {
                    GridViewDataTextColumn c = new GridViewDataTextColumn();
                    c.FieldName = d.ColumnName;
                    c.Caption = d.ColumnName;

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        int len = 1;

                        if (d.ColumnName.IndexOf("金额") > -1 && double.TryParse(ds.Tables[0].Rows[0][d.ColumnName].ToString(), out dvalue))
                        {
                            //PropertiesTextEdit-DisplayFormatString
                            c.PropertiesTextEdit.DisplayFormatString = "0.00";


                        }
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            if (dr[d.ColumnName].ToString().Length > len)
                            {
                                len = dr[d.ColumnName].ToString().Length;
                            }

                        }
                        if (len * 15 > 80)
                        {
                            c.Width = len * 15;
                        }
                        else
                        {
                            c.Width = 80;
                        }
                    }
                    gvList.Columns.Add(c);
                }
            }

            gvList.DataSource = ds.Tables[0];
            gvList.DataBind();

            foreach (GridViewDataTextColumn c in gvList.Columns)
            {
                if (c.FieldName == "到帐金额" || c.FieldName.Trim() == "FEEGUID" || c.FieldName == "大写到帐金额" || c.FieldName == "备注")
                {
                    c.Visible = false;
                }
            }
            double money = 0;
            if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    money += Convert.ToDouble(dr["金额"]);
                }
            }
            PageTotal.InnerText = "总共有：" + gvList.VisibleRowCount + "条数据";
            txtMoney.Text = money.ToString("0.00");
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }
        protected void txtLX_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnChaKan_Click(sender,e);
            if (txtLX.Value.ToString() == "2")
            {
                txtQY.Visible = true;
                TDQY.Visible = true;
            }
            else
            {
                txtQY.Visible = false;
                TDQY.Visible = false;
            }
        }

        protected void UpLoadFile_FileUploadComplete(object sender, DevExpress.Web.FileUploadCompleteEventArgs e)
        {

            if (e.UploadedFile.IsValid)
            {
                string wjmc = e.UploadedFile.FileName;
                string filePath = Path.Combine(Server.MapPath("/UPLOAD/QTGL"), wjmc.Replace(Path.GetExtension(e.UploadedFile.FileName), "") + Path.GetExtension(e.UploadedFile.FileName));    //文件完整物理路径
                e.UploadedFile.SaveAs(filePath);

                e.CallbackData = e.UploadedFile.FileName + "|" + filePath;
                return;
            }
            else
            {
                e.CallbackData = "-1";
                return;
            }
        }

        protected void BtnDaoCHu_Click(object sender, EventArgs e)
        {
            int y = 0;
            if (!int.TryParse(txtYear.Text, out y))
            {
                return;
            }
            DataSet ds = SearchSpHelper.P_QS_QT_DAYINSJDCCX(BusinessTool.WType, Convert.ToInt32(txtYear.Text), Convert.ToInt32(txtLX.Value), Convert.ToInt32(txtQY.Value), null);

            PageTools.ExportExcel(ds.Tables[0]);
        }

        protected void txtYear_TextChanged(object sender, EventArgs e)
        {

            while (txtLX.Items.Count > 3)
            {
                txtLX.Items.RemoveAt(3);
            }
            if (Convert.ToInt32(txtYear.Text.Trim()) >= 201604)
            {
                txtLX.Items.Add("用户帐单");
            }
            else
            {
                txtLX.Items.Add("一般用户帐单");
                txtLX.Items.Add("特殊用户帐单");
            }
        }
        #endregion
    }
}